package com.cinsea.hrm.ext;

import com.cinsea.common.Env;
import com.cinsea.common.IDGenerator;
import com.cinsea.common.JdbcDao;
import com.cinsea.timer.BaseJob;
import com.cinsea.util.DateUtil;
import com.cinsea.util.StringUtil;
import org.quartz.JobExecutionContext;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class KaoqinJob extends BaseJob {
    public void execute(JobExecutionContext jobExecutionContext) {
        //迟到：'4028818336aa84340136aaa67f600012' 早退：'4028818336aa84340136aaa6a3f70013' 旷工：'4028818336aa84340136aaa6eca30014' 漏签：'4028818336aa84340136aaa7669c0015'
        //法定假日：'4028818336aa84340136aa978ef2000a' 调休：'4028818336aa84340136aa985caf000b'
        /*String currentmonth= DateUtil.getCurrentMonth();
        for(String day=currentmonth+"-01";day.compareTo("2012-01-21")<0;day=DateUtil.dayMove(day,1)){
        }*/
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        String currentdate = DateUtil.getCurrentDate();
        boolean beworkday = true;
        boolean beweekend = !DateUtil.isWorkDay(new Date());
        if (beweekend) { //周末且未调休
            List tiaoxiuList = jdbcDao.queryForList("select * from ut_workday where rldate=? and rltype=?", new Object[]{currentdate, "4028818336aa84340136aa985caf000b"});
            if (tiaoxiuList.size() == 0) beworkday = false;
        } else {//工作日且是法定假日
            List holidyList = jdbcDao.queryForList("select * from ut_workday where rldate=? and rltype=?", new Object[]{currentdate, "4028818336aa84340136aa978ef2000a"});
            if (holidyList.size() > 0) beworkday = false;
        }

        if (beworkday) {
            List<Map> userlist = jdbcDao.queryForList("select objid from cs_user where objtype=1 and closed=0 and bemaster=1");

            String starttime1 = "09:00:00";
            String endtime1 = "12:00:00";
            String starttime2 = "13:00:00";
            String endtime2 = "18:00:00";
            String iprange = "";
            String sql = "select starttime1,endtime1,starttime2,endtime2,iprange from ut_kqsz";

            List<Map> kqsettings = jdbcDao.queryForList(sql);
            if (kqsettings.size() > 0) {
                Map kqsetting = kqsettings.get(0);
                String _starttime1 = (String) kqsetting.get("starttime1");
                if (!StringUtil.isEmpty(_starttime1)) starttime1 = _starttime1;
                String _endtime1 = (String) kqsetting.get("endtime1");
                if (!StringUtil.isEmpty(_endtime1)) endtime1 = _endtime1;
                String _starttime2 = (String) kqsetting.get("starttime2");
                if (!StringUtil.isEmpty(_starttime2)) starttime2 = _starttime2;
                String _endtime2 = (String) kqsetting.get("endtime2");
                if (!StringUtil.isEmpty(_endtime2)) endtime2 = _endtime2;
                String _iprange = (String) kqsetting.get("iprange");
                if (!StringUtil.isEmpty(_iprange)) iprange = _iprange;
            }
            for (Map user : userlist) {
                makeKqjg(StringUtil.null2String(user.get("objid")), currentdate, starttime1, starttime2, endtime1, endtime2, iprange);
            }
        }

    }

    public void makeKqjg(String humanid, String date, String starttime1, String starttime2, String endtime1, String endtime2, String iprange) {
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        //删除考勤结果
        jdbcDao.update("delete from ut_kqjg where humanid=? and kqdate=?", new Object[]{humanid, date});
        List<Map> kqjgList;
        if (!StringUtil.isEmpty(iprange)) {
            String sql = "select cardtime from ut_kqjl where humanid=? and carddate=? and cardip like ? order by cardtime";
            kqjgList = jdbcDao.queryForList(sql, new Object[]{humanid, date, iprange + "%"});
        } else {
            String sql = "select cardtime from ut_kqjl where humanid=? and carddate=? order by cardtime";
            kqjgList = jdbcDao.queryForList(sql, new Object[]{humanid, date});

        }
        //生成考勤结果
        String id = IDGenerator.getUnquieID();
        String creator = "11111111111111111111111111111111";//administrator
        String dowid = "4028818336aa84340136aac0b4ac0019";//考勤结果目录
        String currentdatetime = DateUtil.getCurDateTime();
        if (kqjgList.size() == 0) {//旷工

            jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype)  values(?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa6eca30014"});

        } else if (kqjgList.size() == 1) {//漏签
            jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype)   values(?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa7669c0015"});

        } else {
            String starttime = (String) kqjgList.get(0).get("cardtime");
            String endtime = (String) kqjgList.get(kqjgList.size() - 1).get("cardtime");
            if (starttime.compareTo(endtime2) >= 0) {//漏签
                jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype)   values(?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa7669c0015"});
                return;

            } else if (starttime.compareTo(starttime2) > 0) {//迟到:下午签到
                Long minutes = DateUtil.getMinutesBetween(date + " " + endtime1, date + " " + starttime1) + DateUtil.getMinutesBetween(date + " " + starttime, date + " " + starttime2);
                jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype,other)   values(?,?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa67f600012", minutes});
            } else if (starttime.compareTo(endtime1) > 0) {//迟到:中午签到
                Long minutes = DateUtil.getMinutesBetween(date + " " + endtime1, date + " " + starttime1);
                jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype,other)   values(?,?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa67f600012", minutes});
            } else if (starttime.compareTo(starttime1) > 0) {//迟到:上午签到
                Long minutes = DateUtil.getMinutesBetween(date + " " + starttime, date + " " + starttime1);
                jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype,other)   values(?,?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa67f600012", minutes});
            }
			id = IDGenerator.getUnquieID();
            if (endtime.compareTo(starttime1) <= 0) {//漏签
                jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype)  values(?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa7669c0015"});
                return;


            } else if (endtime.compareTo(endtime1) < 0) {//早退:上午签退
                Long minutes = DateUtil.getMinutesBetween(date + " " + endtime2, date + " " + starttime2) + DateUtil.getMinutesBetween(date + " " + endtime1, date + " " + endtime);
                jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype,other)   values(?,?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa6a3f70013", minutes});


            } else if (endtime.compareTo(starttime2) < 0) {//早退:中午签退
                Long minutes = DateUtil.getMinutesBetween(date + " " + endtime2, date + " " + starttime2);
                jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype,other)   values(?,?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa6a3f70013", minutes});


            } else if (endtime.compareTo(endtime2) < 0) {//早退:下午签退
                Long minutes = DateUtil.getMinutesBetween(date + " " + endtime2, date + " " + endtime);
                jdbcDao.update("insert into ut_kqjg(id,processid,dowid,finished,deleted,createuserid,modifyuserid,createdatetime,modifydatetime,humanid,kqdate,kqtype,other)   values(?,?,?,?,?,?,?,?,?,?,?,?,?) ", new Object[]{id, id, dowid, -1, 0, creator, creator, currentdatetime, currentdatetime, humanid, date, "4028818336aa84340136aaa6a3f70013", minutes});


            }

        }

    }
}
